Skip to content

Try: Data: Cache selector results by state via WeakMap - #6458

Closed
aduth wants to merge 2 commits into
masterfrom
try/selector-weak-map-cache
Closed

aduth wants to merge 2 commits into
masterfrom
try/selector-weak-map-cache

Conversation

@aduth

@aduth aduth commented Apr 27, 2018

Copy link
Copy Markdown
Member

This pull request seeks to explore an option for aggressive selector caching on every selector when called with the same state. It does so by tracking an EquivalentKeyMap per state object by WeakMap (meaning unreferenced states are garbage collected), where each entry is a combination of selector name and arguments set.

This particularly optimizes for repeated calls to the same selector with the same state value, without requiring explicit memoization by createSelector. The latter may still be useful for more granular caching across distinct state values.

@aduth aduth added [Status] In Progress Tracking issues with work in progress [Type] Performance Related to performance efforts labels Apr 27, 2018
@aduth
aduth requested review from atimmer and youknowriad July 16, 2018 14:37
@aduth

aduth commented Jul 16, 2018

Copy link
Copy Markdown
Member Author

I've been thinking about this one again lately, as it seems particularly wasteful we call the same selectors repeatedly during the same tick on the same state. It's less impactful if the selector is memoized, but not all selectors are memoized. Having a more broad-yet-simple cache could also avoid the need for createSelector merely to satisfy the same intent, which itself could be overkill (or worse, open the possibility for regressions / bugs).

My main concern would be that for a very simple selector, there's probably more overhead merely in the cache lookup than what actually occurs in the selector itself.

Does this seem like something worth continuing to explore? Or should we close it?

@atimmer

atimmer commented Oct 23, 2018

Copy link
Copy Markdown
Member

I think we should close this. Enabling caching by default can also result in unexpected behavior. It is probably better to it work uncached by default and only add caching when it results in a performance on the actual bottlenecks of the application.

@aduth

aduth commented Oct 23, 2018

Copy link
Copy Markdown
Member Author

Enabling caching by default can also result in unexpected behavior.

Do you have an example in mind of something which could result in unexpected behavior? I think this could only occur if state is mutated directly, which is definitely not advisable nor intended to be supported, and likely already would have unexpected behavior anyways.

The main idea here (perhaps not as currently implemented) is that when state changes, we have many many components wrapped with withSelect which are calling the same selectors with identical state. If we hold a weak reference mapping between state and the result of a selector call given that state, we can eliminate the need for all but one of those calls.

The main hold-up as I see it is in accounting for arguments variations, and whether that can be considered performant-enough to make the optimization worthwhile. Alternatively, we could limit the application of caching to only those selectors which are called without any arguments except for the state object.

@atimmer

atimmer commented Oct 23, 2018

Copy link
Copy Markdown
Member

I should have linked it, but I was mainly thinking about the issues mentioned here: https://redux.js.org/recipes/computingderiveddata#sharing-selectors-across-multiple-components. I don't know if those specific issues are relevant here, but I know that with caching there is always an extra layer of complexity which you need to think about when implementing.

A different issue I encountered was where the memoized function would be called with different object references, which wouldn't be deduced, which resulted in memory leaks. Again, I'm not sure if that is relevant here. I just want to note that a caching layer always introduces another layer of complexity people need to think about.

@aduth

aduth commented Oct 23, 2018

Copy link
Copy Markdown
Member Author

If it can't be done without imposing some awareness on consuming developers, then I agree it shouldn't be introduced. But I also don't think it'll require any awareness.

A different issue I encountered was where the memoized function would be called with different object references, which wouldn't be deduced, which resulted in memory leaks.

WeakMap is the crucial piece here, and works perfectly with state as the key.

@atimmer

atimmer commented Oct 23, 2018

Copy link
Copy Markdown
Member

I don't feel strongly about it either way, just mentioning my concerns.

@kwight

kwight commented Mar 9, 2019

Copy link
Copy Markdown

Is this still under consideration? If so, are there any testing instructions, or examples others can try to decide which way forward (abandon, cache all, cache some) would make sense?

@aduth

aduth commented Mar 11, 2019

Copy link
Copy Markdown
Member Author

I think there's still value in this, though it's a tricky balance between the overhead of the cache lookup vs. the cost of the raw underlying selector.

In any case, I do not expect to revisit it in the immediate future. We can reopen it in the future if it becomes a pressing need.

@aduth aduth closed this Mar 11, 2019
@aduth
aduth deleted the try/selector-weak-map-cache branch March 11, 2019 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Status] In Progress Tracking issues with work in progress [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants